﻿using System;
using XRM.Security.DataAccess.Interfaces;
using XRM.Security.DataAccess.Enums;
namespace XRM.Security.DataAccess.Helpers
{
    public static class DataModelUtils
    {
        public static int SoonExpireDaysCount = 30;

        public static CertStatus Status( this IUserCertStatus cert )
        {
            return cert.UserStatus != (int)UserStatus.Work ? CertStatus.NotValid : Status((ICertStatus)cert);
        }

        public static CertStatus Status( this ICertStatus cert )
        {
            var today = DateTime.Today;
            if ( cert.ToDate < today )
                return CertStatus.NotValid;
            return cert.ToDate <= today.AddDays( SoonExpireDaysCount + 1 ) ? CertStatus.ExpireSoon : CertStatus.Valid;
        }

        public static string FullName(this IFio obj)
        {
            if ( obj == null )
                return null;
            return obj.MotherName != null ? String.Format("{0} {1} {2}", obj.LastName, obj.FirstName, obj.MotherName)
                : String.Format("{0} {1}", obj.LastName, obj.FirstName);
        }

        public static string ShortName( this IFio obj )
        {
            if ( obj == null )
                return null;
            return obj.MotherName != null ? String.Format( "{0} {1}.{2}.", obj.LastName, obj.FirstName[0], obj.MotherName[0] )
                : String.Format( "{0} {1}", obj.LastName, obj.FirstName );
        }
    }
}